/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package managedBean;

import Merlion.MRP.bean.DemandManagementBeanRemote;
import Merlion.SCM.bean.InventoryItemsBeanRemote;
import Merlion.util.state.InventoryState;
import Merlion.util.state.SOPBreakdownState;
import Merlion.util.state.SalesOperationPlanState;
import java.util.ArrayList;
import java.util.List;
import javax.ejb.EJB;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;
import javax.faces.event.PhaseEvent;
import org.primefaces.event.RowEditEvent;

/**
 *
 * @author cute_jes91
 */
@ManagedBean(name = "SalesAndOperationManagedBean")
@ViewScoped
public class SalesAndOperationManagedBean {

    private int period;
    private Integer yearInt;
    private int beginningInventory;
    private int monthInt;
    private int salesForecast;
    private int productionPlan;
    private int inventory;
    private boolean finalized;
    private int nWorkingDays;
    private int capacity;
    private int utilization;
    private int ratio1;
    private int ratio2;
    private int ratio3;
    private int ratio4;
    private List<SalesOperationPlanState> selectedSOP;
    private SalesOperationPlanState[] selectedSOPs;
    private SalesOperationPlanState SOP;
    private List<InventoryState> productList;
    private List<Object[]> productQuantityMap = new ArrayList<Object[]>();
    //private List<SOPBreakdownState> breakdownss;
    private List<SOPBreakdownState> breakdowns;
    //get product details
    private String name1;
    private String name2;
    private String name3;
    private String name4;
    private Long id1;
    private Long id2;
    private Long id3;
    private Long id4;
    //end of getting product details
    private Long productId;
    private int productQuantity;
    private Object[] breakdown = new Object[2];
    @EJB
    private InventoryItemsBeanRemote InventoryItemsBean;
    @EJB
    private DemandManagementBeanRemote dmb;

    /** Creates a new instance of SalesAndOperationManagedBean */
    public SalesAndOperationManagedBean() {
    }

    public void initView(PhaseEvent event) {
        System.out.println("**SalesAndOperationManagedBean: initView()");


        FacesContext.getCurrentInstance().getExternalContext().getSession(true);

        if (productList == null) {
            productList = InventoryItemsBean.viewProducts();
            productList.size();
            name1 = getProductList().get(0).getName();
            name2 = getProductList().get(1).getName();
            name3 = getProductList().get(2).getName();
            name4 = getProductList().get(3).getName();

            id1 = getProductList().get(0).getItemId();
            id2 = getProductList().get(1).getItemId();
            id3 = getProductList().get(2).getItemId();
            id4 = getProductList().get(3).getItemId();

        }

        if (selectedSOP == null) {
            System.out.println("SELECTED SOP IS NULL");
            // selectedSOP = dmb.generateMonthlyPlannedDemands(startYear, startMonth, endYear, endMonth, beginningInventory, productQuantityMap, false, true);
        }

    }

    public void onEditRow(RowEditEvent event) {
        System.out.println("**SalesAndOperationManagedBean :  onEditRow()");

        try {
            SalesOperationPlanState sop = (SalesOperationPlanState) event.getObject();

            if (sop != null) {
                int year = sop.getYearInt();
                int month = sop.getMonthInt();
                int productionPlans = sop.getProductionPlan();

                SOP = dmb.updateSingleMonthlySalesOperationPlan(year, month, productionPlans);
              //  this.setUtilization((int) SOP.getUtilization());
                sop.setUtilization((int)SOP.getUtilization());
                sop.setProductionPlan(SOP.getProductionPlan());
                sop.setFinalized(SOP.isFinalized());
            
                System.out.println("**SalesAndOperationManagedBean onEditRow:  SOP is successfully edited, finalize = " + sop.isFinalized());

            } else {
                System.out.println("**SalesAndOperationManagedBean: No SOP data found. onEditRow() failed.");
            }
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    public void doHandleUtilization(){
        System.out.println("**SalesAndOperationManagedBean : doHandleUtilization");
        if(SOP!=null){
            SOP.setUtilization((int)SOP.getUtilization());
            SOP.setProductionPlan(SOP.getProductionPlan());
            //set finalized later!!!!s
        }
    }
    public List<Object[]> listOfRatio(int ratio1, int ratio2, int ratio3, int ratio4) {
        FacesContext.getCurrentInstance().getExternalContext().getSession(true);

        System.out.println("**SalesAndOperationManagedBean : listOfRatio()");

        if (productQuantityMap != null) {
            productQuantityMap.clear();
        }
        Object[] object1 = new Object[2];
        object1[0] = id1;
        object1[1] = ratio1;

        System.out.println("RATIOOOOOO : " + object1[1]);


        Object[] object2 = new Object[2];
        object2[0] = id2;
        object2[1] = ratio2;

        Object[] object3 = new Object[2];
        object3[0] = id3;
        object3[1] = ratio3;

        Object[] object4 = new Object[2];
        object4[0] = id4;
        object4[1] = ratio4;

        productQuantityMap.add(object1);
        productQuantityMap.add(object2);
        productQuantityMap.add(object3);
        productQuantityMap.add(object4);
        return productQuantityMap;
    }

    public void doGenerate(ActionEvent event) {
        System.out.println("**SalesAndOperationManagedBean : doGenerate()");
        try {
            FacesContext.getCurrentInstance().getExternalContext().getSession(true);

            if (ratio1 != 0 && ratio2 != 0 && ratio3 != 0 && ratio4 != 0) {
                productQuantityMap = listOfRatio(ratio1, ratio2, ratio3, ratio4);
            }


            System.out.println("ID - RATIO " + id1 + "," + ratio1);
            selectedSOP = dmb.generateSalesOperationPlanByPeriod(period, yearInt, productQuantityMap);
            //selectedSOP= dmb.generateMonthlyPlannedDemands(startYear, startMonth, endYear, endMonth, beginningInventory, productQuantityMap, false, false);
            this.setSelectedSOP(selectedSOP);
            for (int i = 0; i < 1; i++) {
                SOP = selectedSOP.get(0);
                this.setBeginningInventory(SOP.getBeginningInventory());
            }

        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    /* public void doView(ActionEvent event) {
    System.out.println("**SalesAndOperationManagedBean: doView()");
    selectedSOP = dmb.viewSalesOperationPlanByPeriod(period, yearInt);
    this.setSelectedSOP(selectedSOP);
    }
    
    public void doFinalize(ActionEvent event) {
    System.out.println("**SalesAndOperationManagedBean: doFinalize()");
    
    selectedSOP = dmb.finalizeSalesOperationPlanByPeriod(period, yearInt, productQuantityMap);
    String quarter;
    if (period == 11) {
    quarter = "Q1";
    } else if (period == 12) {
    quarter = "Q2";
    } else if (period == 13) {
    quarter = "Q3";
    } else if (period == 14) {
    quarter = "Q4";
    } else {
    quarter = "Null";
    }
    try {
    FacesContext.getCurrentInstance().addMessage(null, new FacesMessage("Successful", "Forecast for period " + quarter + " in year " + yearInt + " has been finalized"));
    } catch (Exception ex) {
    ex.printStackTrace();
    }
    }*/
    public void doUpdateAndFinalize(ActionEvent event) {
        System.out.println("**SalesAndOperationManagedBean : doUpdateAndFinalize()");
        String quarter;
        if (period == 11) {
            quarter = "Q1";
        } else if (period == 12) {
            quarter = "Q2";
        } else if (period == 13) {
            quarter = "Q3";
        } else if (period == 14) {
            quarter = "Q4";
        } else {
            quarter = "Null";
        }

        if (selectedSOPs != null) {
            for (Object sop : selectedSOPs) {
                SalesOperationPlanState sp = (SalesOperationPlanState) sop;
                yearInt = sp.getYearInt();
                monthInt = sp.getMonthInt();
                productionPlan = sp.getProductionPlan();
                SOP = dmb.updateSingleMonthlySalesOperationPlan(yearInt, monthInt, productionPlan);
                sp.setUtilization(SOP.getUtilization());
                sp.setProductionPlan(SOP.getProductionPlan());
                sp.setFinalized(SOP.isFinalized());
               // System.out.println("STATUS "+SOP.isFinalized());
                System.out.println(SOP.getUtilization());
                FacesContext.getCurrentInstance().addMessage(null, new FacesMessage("Successful", "Forecast for period " + quarter +"(month :"+ monthInt+")" + " in year " + yearInt + " has been finalized"));
               selectedSOP = dmb.viewSalesOperationPlanByPeriod(period, yearInt);
               this.setSelectedSOP(selectedSOP);
            }
        }
    }

    public int getBeginningInventory() {
        return beginningInventory;
    }

    public void setBeginningInventory(int beginningInventory) {
        this.beginningInventory = beginningInventory;
    }

    public int getSalesForecast() {
        return salesForecast;
    }

    public void setSalesForecast(int salesForecast) {
        this.salesForecast = salesForecast;
    }

    public int getProductionPlan() {
        return productionPlan;
    }

    public void setProductionPlan(int productionPlan) {
        this.productionPlan = productionPlan;
    }

    public int getInventory() {
        return inventory;
    }

    public void setInventory(int inventory) {
        this.inventory = inventory;
    }

    public int getCapacity() {
        return capacity;
    }

    public void setCapacity(int capacity) {
        this.capacity = capacity;
    }

    public int getUtilization() {
        return utilization;
    }

    public void setUtilization(int utilization) {
        this.utilization = utilization;
    }

    public DemandManagementBeanRemote getDmb() {
        return dmb;
    }

    public void setDmb(DemandManagementBeanRemote dmb) {
        this.dmb = dmb;
    }

    public int getMonthInt() {
        return monthInt;
    }

    public void setMonthInt(int monthInt) {
        this.monthInt = monthInt;
    }

    public List<SalesOperationPlanState> getSelectedSOP() {
        return selectedSOP;
    }

    public void setSelectedSOP(List<SalesOperationPlanState> selectedSOP) {
        this.selectedSOP = selectedSOP;
    }

    public int getRatio1() {
        return ratio1;
    }

    public void setRatio1(int ratio1) {
        this.ratio1 = ratio1;
    }

    public int getRatio2() {
        return ratio2;
    }

    public void setRatio2(int ratio2) {
        this.ratio2 = ratio2;
    }

    public int getRatio3() {
        return ratio3;
    }

    public void setRatio3(int ratio3) {
        this.ratio3 = ratio3;
    }

    public int getRatio4() {
        return ratio4;
    }

    public void setRatio4(int ratio4) {
        this.ratio4 = ratio4;
    }

    public List<InventoryState> getProductList() {
        return productList;
    }

    public void setProductList(List<InventoryState> productList) {
        this.productList = productList;
    }

    public String getName1() {
        return name1;
    }

    public void setName1(String name1) {
        this.name1 = name1;
    }

    public String getName2() {
        return name2;
    }

    public void setName2(String name2) {
        this.name2 = name2;
    }

    public String getName3() {
        return name3;
    }

    public void setName3(String name3) {
        this.name3 = name3;
    }

    public String getName4() {
        return name4;
    }

    public void setName4(String name4) {
        this.name4 = name4;
    }

    public InventoryItemsBeanRemote getInventoryItemsBean() {
        return InventoryItemsBean;
    }

    public void setInventoryItemsBean(InventoryItemsBeanRemote InventoryItemsBean) {
        this.InventoryItemsBean = InventoryItemsBean;
    }

    public Long getId1() {
        return id1;
    }

    public void setId1(Long id1) {
        this.id1 = id1;
    }

    public Long getId2() {
        return id2;
    }

    public void setId2(Long id2) {
        this.id2 = id2;
    }

    public Long getId3() {
        return id3;
    }

    public void setId3(Long id3) {
        this.id3 = id3;
    }

    public Long getId4() {
        return id4;
    }

    public void setId4(Long id4) {
        this.id4 = id4;
    }

    public int getnWorkingDays() {
        return nWorkingDays;
    }

    public void setnWorkingDays(int nWorkingDays) {
        this.nWorkingDays = nWorkingDays;
    }

    public SalesOperationPlanState getSOP() {
        return SOP;
    }

    public void setSOP(SalesOperationPlanState SOP) {
        this.SOP = SOP;
    }

    public Long getProductId() {
        return productId;
    }

    public void setProductId(Long productId) {
        this.productId = productId;
    }

    public int getProductQuantity() {
        return productQuantity;
    }

    public void setProductQuantity(int productQuantity) {
        this.productQuantity = productQuantity;
    }

    public List<Object[]> getProductQuantityMap() {
        return productQuantityMap;
    }

    public void setProductQuantityMap(List<Object[]> productQuantityMap) {
        this.productQuantityMap = productQuantityMap;
    }

    public Object[] getBreakdown() {
        return breakdown;
    }

    public void setBreakdown(Object[] breakdown) {
        this.breakdown = breakdown;
    }

    public int getPeriod() {
        return period;
    }

    public void setPeriod(int period) {
        this.period = period;
    }

    public Integer getYearInt() {
        return yearInt;
    }

    public void setYearInt(Integer yearInt) {
        this.yearInt = yearInt;
    }

    public SalesOperationPlanState[] getSelectedSOPs() {
        return selectedSOPs;
    }

    public void setSelectedSOPs(SalesOperationPlanState[] selectedSOPs) {
        this.selectedSOPs = selectedSOPs;
    }

    public boolean isFinalized() {
        return finalized;
    }

    public void setFinalized(boolean finalized) {
        this.finalized = finalized;
    }
}
